home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 32 / cadence.zip / VOL2NO4.ZIP / CADENCE.LSP next >
Text File  |  1986-03-12  |  11KB  |  332 lines

  1. ;;;; These variables are defined globally  
  2. (setq RAD2DEG 57.29578)  ; degrees per radian
  3. (setq LINESET (ssadd))   ; "block create" selection set
  4. (setq COUNTER 0)         ; "block create" name sequencing device
  5.  
  6. ;;;
  7. ;;; name: C:DEFBLOCK
  8. ;;;
  9. ;;; synopsis: Function implemented as an AutoCAD command.  This function
  10. ;;;           validates the "tee" geonmetry of two selected lines.  If valid
  11. ;;;           creates and inserts a block based on the relationship of the 
  12. ;;;           selected lines layer names.
  13. ;;;
  14. ;;; input: Queries the user for a mainline and a branch line. 
  15. ;;;
  16. ;;; return value: none - implemented as an AutoCAD commnad.
  17. ;;;
  18. (defun C:DEFBLOCK ( / namlist mainline branchline intersection symbol-def)
  19.                       ;inpt wpt1 wpt2 wpt3 wpt4 a1 a2 a3 a4 a5 a6 a7 a8)
  20.  
  21.   ;; Query uset to select main line
  22.   (setq mainline (getline "\nSelect main leg: "))
  23.  
  24.   (setvar "cmdecho" 0)
  25.   (setvar "blipmode" 0)
  26.   ;;If a valid line, get the branch line
  27.   (if (/= mainline nil)
  28.     (progn
  29.       (setq branchline (getline "\nSelect branch leg: "))
  30.  
  31.       ;;If a valid line, continue
  32.       (if (/= branchline nil)
  33.         (progn
  34.  
  35.           ;;Calculate the intersection of the mainline and the branch line 
  36.           (setq intersection (calc-inters mainline branchline))
  37.  
  38.           ;;If intersection exists, continue
  39.           (if (/= intersection nil)
  40.             (progn
  41.  
  42.               ;;Calculate the symbol definition points
  43.               (setq inpt (car intersection))
  44.               (setq wpt1 (polar inpt (cadr intersection) 0.1875))
  45.               (setq wpt2 (polar inpt (+ (cadr intersection) pi) 0.1875))
  46.               (setq wpt3 (polar inpt (caddr intersection) 0.1875))
  47.               (setq wpt4 (polar inpt (+ (caddr intersection) pi) 0.1875))
  48.               (setq a1 (polar wpt1 (+ (caddr intersection) pi) 0.15625))
  49.               (setq a2 (polar a1 (caddr intersection) 0.3125))
  50.               (setq a3 (polar wpt2 (+ (caddr intersection) pi) 0.15625))
  51.               (setq a4 (polar a3 (caddr intersection) 0.3125))
  52.               (setq a5 (polar wpt3 (+ (cadr intersection) pi) 0.15625))
  53.               (setq a6 (polar a5 (cadr intersection) 0.3125))
  54.               (setq a7 (polar wpt4 (+ (cadr intersection) pi) 0.15625))
  55.               (setq a8 (polar a7 (cadr intersection) 0.3125))
  56.  
  57.               ;;"BREAK" the main and branch lines
  58.               (break-line (car mainline) wpt1 wpt2)
  59.               (break-line (car branchline) wpt3 wpt4)
  60.  
  61.               ;;Determine which symbol is to be drawn
  62.               (setq COUNTER (1+ COUNTER))
  63.               (setq symbol-def (apply-tee-rules mainline branchline))
  64.  
  65.               ;;Draw the symbol, make it a block, insert the new block
  66.               (setq namlist (build-block (car symbol-def)))
  67.               (command "BLOCK" (cadr symbol-def) inpt LINESET "")
  68.               (command "INSERT" (cadr symbol-def) inpt "1" "1" "0")
  69.             );progn
  70.             (prompt "\nSelected line segments are invalid.");bad intersection
  71.           );if
  72.         );progn
  73.         (prompt "\nBranch line selection invalid.");bad branch line
  74.       );if
  75.     );progn
  76.     (prompt "\nMain line selection invalid.");bad main line
  77.   );if
  78.  
  79.   ;;empties the selection set so it can be reused
  80.   (clean-ss namlist)
  81.   (graphscr)
  82.   (prompt "\nCommand")
  83.   ':
  84. );end C:DEFBLOCK
  85.  
  86. ;;;
  87. ;;; name: getline
  88. ;;;
  89. ;;; synopsis: Patterned after the AutoLISP "get" functions. Prompts the user
  90. ;;;           to select a line.
  91. ;;;
  92. ;;; syntax: (getline <prompt>)
  93. ;;;         <prompt> - A string to be used as a prompt.
  94. ;;;
  95. ;;; return value: A list containing the line's database name, the start point 
  96. ;;;               of the line, the endpoint of the line, and the line's layer.
  97. ;;;
  98. (defun getline (querry / objname lname llist)
  99.  
  100.   ;;Retrieve selected entity name from database
  101.   (setq objname (entsel querry))
  102.  
  103.   ;;If no entity selected prompt error message and return nil, Else continue
  104.   (if (= objname nil)
  105.     (progn
  106.       (prompt "\nNo object selected")
  107.       (eval nil)
  108.     );progn
  109.     (progn
  110.  
  111.       ;;Retrieve selected entity association list from database 
  112.       (setq lname (car objname))
  113.       (redraw lname 3)
  114.       (setq llist (entget lname))
  115.  
  116.       ;;If entity is a line continue, Else prompt error and return nil.
  117.       (if (= (cdr (assoc '0 llist)) "LINE")
  118.         (progn
  119.  
  120.           ;;Build the return list.
  121.           (redraw lname 4)
  122.           (list lname 
  123.                 (cdr (assoc '10 llist)) 
  124.                 (cdr (assoc '11 llist))
  125.                 (cdr (assoc '8 llist)) 
  126.           );list
  127.         );progn
  128.         (progn
  129.           (prompt "\nSelected object is not a line.")
  130.           (redraw lname 4)
  131.           (eval nil)
  132.         );progn
  133.       );if
  134.     );progn
  135.   );if
  136. ); end getline
  137.  
  138. ;;;
  139. ;;; name: calc-inters
  140. ;;;
  141. ;;; synopsis: Computes the intersection of lines and calculates the direction
  142. ;;;           of the lines based on that intersection.
  143. ;;;
  144. ;;; syntax: (calc-inters <getline-list1> <getline-list2>)
  145. ;;;  <getline-list1> - the main line {see getline for order of getline list
  146. ;;;  <getline-list2> - the branch line {see getline for order of getline list
  147. ;;;
  148. ;;; return value: A list consisting of a point (the intersection point), the 
  149. ;;;               direction of the mainline from the point of intersection, 
  150. ;;;               and the direction of the branch line from the point of 
  151. ;;;               intersection.
  152. ;;;
  153. (defun calc-inters (mainline branchline / xpt mainhead brchhead)
  154.  
  155.   ;;Calculate the point of intersection
  156.   (setq xpt (inters (cadr mainline) (caddr mainline) 
  157.                     (cadr branchline) (caddr branchline) nil))
  158.  
  159.   ;;Calculate direction of the lines.
  160.   (if (< (distance xpt (cadr mainline)) 0.025)
  161.     (setq mainhead (angle xpt (caddr mainline)))
  162.     (setq mainhead (angle xpt (cadr mainline)))
  163.   );if
  164.   (if (< (distance xpt (cadr branchline)) 0.025)
  165.     (setq brchhead (angle xpt (caddr branchline)))
  166.     (setq brchhead (angle xpt (cadr branchline)))
  167.   );if
  168.  
  169.   ;;Build the return list
  170.   (list xpt mainhead brchhead)
  171. ); end calc-inters
  172.  
  173. ;;;
  174. ;;; name: break-line
  175. ;;;
  176. ;;; synopsis: BREAK's a line
  177. ;;;
  178. ;;; syntax: (break-line <ename> <point1> <point2>)
  179. ;;;         <ename> - database name on entity to break
  180. ;;;         <point1> - a point list representing a point on the "BREAK".
  181. ;;;         <point2> - a point list representing a point on the "BREAK".
  182. ;;;
  183. ;;; return value: nil
  184. ;;;
  185. ;;; side effect - Specified line is broken
  186. ;;;
  187. (defun break-line (ename pt1 pt2 / editset elist c1 c2 pt1 pt2)
  188.  
  189.   ;;Calculate the points of a window using the first point
  190.   (setq c1 (list (+ (car pt1) 0.05) (+ (cadr pt1) 0.05)))
  191.   (setq c2 (list (- (car pt1) 0.05) (- (cadr pt1) 0.05)))
  192.  
  193.   ;;Check to see if there are lines to edit
  194.   (setq editset (ssget "C" c1 c2))
  195.  
  196.   ;;If lines are not present, try again with the second point
  197.   (if (= editset nil 0)
  198.     (progn
  199.       (setq c1 (list (+ (car pt2) 0.05) (+ (cadr pt2) 0.05)))
  200.       (setq c2 (list (- (car pt2) 0.05) (- (cadr pt2) 0.05)))
  201.       (setq editset (ssget "C" c1 c2))
  202.  
  203.       ;;If still no lines prompt error and return, Else execute break.
  204.       (if (= editset nil)
  205.         (prompt "\nInvalid edit points. Break not preformed")
  206.         (progn
  207.  
  208.           ;;Build list like that returned by entsel using the first point
  209.           ;;and select objects with this list
  210.           (setq elist (list ename pt2))
  211.           (command "BREAK" elist pt1)
  212.         );progn
  213.       );if
  214.     );progn
  215.     (progn
  216.  
  217.       ;;Build list like that returned by entsel using the first point
  218.       ;;and select objects with this list
  219.       (setq elist (list ename pt1))
  220.       (command "BREAK" elist pt2)
  221.     );progn
  222.   );if
  223. );end break-line
  224.  
  225. ;;;
  226. ;;; name: apply-tee-rules
  227. ;;;
  228. ;;; synopsis: Computes the relationship between two intersecting lines and based
  229. ;;;           on that relationship creates the correct block.
  230. ;;;
  231. ;;; syntax: (apply-tee-rules <getline-list1> <getline-list2>)
  232. ;;;         <getline-list1> - the mainline
  233. ;;;         <getline-list1> - the branchline
  234. ;;;
  235. ;;; return value: A list consisting of the order of point insertion and the computed
  236. ;;;         block name.
  237. ;;;
  238. (defun apply-tee-rules (main branch)
  239.   (cond
  240.  
  241.     ;;If both layer names are equal to "0"
  242.     ( (and (equal (cadddr main) "0") (equal (cadddr branch) "0"))
  243.       (list '(wpt1 wpt3 wpt2 wpt4 wpt1)
  244.              (strcat "TYPE0" (itoa COUNTER)))
  245.     );
  246.  
  247.     ;;If both layer names are equal to "1"
  248.     ( (and (equal (cadddr main) "1") (equal (cadddr branch) "1"))
  249.       (list '(a1 a2 a6 a5 a4 a3 a7 a8 a1)
  250.              (strcat "TYPE1" (itoa COUNTER)))
  251.     );
  252.     
  253.     ;;Default case
  254.     ( T
  255.       (list '(a1 a2 inpt a6 a5 inpt a4 a3 a1)
  256.              (strcat "TYPET" (itoa COUNTER)))
  257.     );
  258.   );cond
  259. );
  260.  
  261. ;;;
  262. ;;; name: build-block
  263. ;;;
  264. ;;; synopsis: Executes the "LINE" command on the list of points returned by
  265. ;;;           apply-tee-rules.
  266. ;;;
  267. ;;; syntax: (build-block <list>)
  268. ;;;         <list> - list of point names
  269. ;;;
  270. ;;; return value: a list of line entity names drawn
  271. ;;;         
  272. ;;; side-effect: LINESET the global selection set filled with line entity
  273. ;;;              drawn
  274. ;;; 
  275. (defun build-block (linlst / apt bpt namlist ename)
  276.  
  277.   ;;Get first point and trim the list
  278.   (setq apt (car linlst))
  279.   (setq linlst (cdr linlst))
  280.  
  281.   ;;while the first point of the linlst is not nil, continue
  282.   (while (setq bpt (car linlst))
  283.  
  284.     ;;The elements of linlst are actually point names and must be evaluated
  285.     ;;first to access thier values.
  286.     (command "LINE" (eval apt) (eval bpt))
  287.     (command "")
  288.     
  289.     ;;Get the entity name of the line segment drawn and add it to the selection
  290.     ;;list and the auxillary name list.
  291.     (setq ename (entlast))
  292.     (ssadd ename LINESET)
  293.     (setq namlist (cons ename namlist) apt bpt linlst (cdr linlst))
  294.   );while
  295.  
  296.   ;;Return the list of entity names
  297.   (eval 'namlist)
  298. );end build-block
  299.  
  300. ;;;
  301. ;;; name: clean-ss
  302. ;;;
  303. ;;; synopsis: Resets the global "create block" selection set to empty.
  304. ;;;
  305. ;;; syntax: (clean-ss <list>)
  306. ;;;          <list> - list of entity names to be removed from LINESET.
  307. ;;;
  308. ;;; return value: nil
  309. ;;;
  310. ;;; side effect: LINESET empty.
  311. ;;;
  312. (defun clean-ss (namlist / curname)
  313.   (if (/= namlist nil)
  314.     (progn
  315.  
  316.       ;;Get first entity name
  317.       (setq curname (car namlist))
  318.       (while (setq namlist (cdr namlist))
  319.  
  320.         ;;Using entity name from namlist, delete that entity from the selection
  321.         ;;set LINESET
  322.         (ssdel curname LINESET)
  323.         (setq curname (car namlist))
  324.       );while
  325.       (ssdel curname LINESET)
  326.     );progn
  327.   );if
  328. );end clean-ss
  329.  
  330. (prompt "Sample design envrionment loaded")
  331. ':
  332.